//
//  OCNetworkingTool.m
//  ocVB
//
//  Created by apple on 2016/10/27.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "OCNetworkingTool.h"
#import "OCUserInfoModel.h"



@implementation OCNetworkingTool

+(instancetype)sharedManager{
    static OCNetworkingTool * instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [[OCNetworkingTool alloc] init];
        instance.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html",@"text/plain", nil];;
    });
    return  instance;
}

/**
 自定义请求网络数据类

 @param way       请求方式
 @param URLString URL
 @param parameter 参数
 @param success   成功回调
 @param failure   失败回调
 */
- (void)requestData:(requestMethod)way URLString:(NSString *)URLString parameters:(id)parameter success:(void(^)(id))success failure:(void(^)(NSError *))failure{
    if (way == requestMethodGET) {
        [self GET:URLString parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];

    }else{
        [self POST:URLString parameters:parameter progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            success(responseObject);
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            failure(error);
        }];
    }
}
///获取用户的tocken
-(void)getUserTocken:(NSString *)code success:(void(^)(id))success failure:(void(^)(NSError *))failure{
    NSLog(@"---%@",code);
    NSString *urlString = @"https://api.weibo.com/oauth2/access_token";
    NSDictionary *dict = @{
                           @"client_id": OCAPPKEY,
                           @"client_secret": OCAPPSECRET,
                           @"grant_type": @"authorization_code",
                           @"code": code,
                           @"redirect_uri": OCREDIRECTURI
                           };
    
    [self requestData:requestMethodPOST URLString:urlString parameters:dict success:success failure:failure];
    
}

  ///获取用户信息
- (void)oauthLoadUserInfo:(OCUserInfoModel *)model success:(void(^)(id))success failure:(void(^)(NSError *))failure
{
    NSString *URLstr = @"https://api.weibo.com/2/users/show.json";
    NSLog(@"token-- %@,, uid -- %@",model.access_token,model.uid);
    NSDictionary *params = @{
                             @"access_token":model.access_token,
                             @"uid":model.uid
                             };
    [self requestData:requestMethodGET URLString:URLstr parameters:params success:success failure:failure];
   
}
    


@end
